package com.jzr.board.presenter.impl

import android.content.Context
import com.jzr.board.api.Api
import com.jzr.board.base.BasePresenter
import com.jzr.board.bean.*
import com.jzr.board.bean.body.UpdateCareLabelBody
import com.jzr.board.bean.body.UpdateNursingBody
import com.jzr.board.presenter.contract.PatientListFragmentContract
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class PatientListFragmentImpl : BasePresenter<PatientListFragmentContract.View>(),
    PatientListFragmentContract.Presenter<PatientListFragmentContract.View> {


    override fun getPatientList(context: Context?, vararg s: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getPatientList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<PatientListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: PatientListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getPatientListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getNurseListBean(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getNurseCodeList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<NurseListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: NurseListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getNurseListBeanSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )

    }

    override fun getCategoryListBean(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getCategoryList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<CategoryListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: CategoryListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getCategoryListBeanSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )

    }

    override fun getCareLabelList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getCareLabelList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<CareLabelListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: CareLabelListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getCareLabelListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getUpdateCareLabel(context: Context?, record: UpdateCareLabelBody?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getUpdateCareLabel(record)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<BaseBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: BaseBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getUpdateCareLabelSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getPatientCareLabelList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getPatientCareLabelList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<PatientCareLabelListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: PatientCareLabelListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getPatientCareLabelListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getUpdateNursing(context: Context?, body: UpdateNursingBody?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getUpdateNursing(body!!)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<BaseBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: BaseBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getUpdateNursingSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getFeeRecordList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getFeeRecordList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<FeeRecordListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: FeeRecordListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0) {
                            mView?.getFeeRecordListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getTestReportList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getTestReportList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TestReportListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: TestReportListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0 && t.data!=null) {
                            mView?.getTestReportListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getInspectReportList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getInspectReportList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<InspectReportListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: InspectReportListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0 && t.data!=null) {
                            mView?.getInspectReportListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getOrderList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getOrderList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<OrderListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: OrderListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0 && t.data!=null) {
                            mView?.getOrderListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getTemperatureList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getTemperatureList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TemBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: TemBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0 && t.data!=null) {
                            mView?.getTemperatureListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

    override fun getBloodPressList(context: Context?, vararg s: String) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getBloodPressList(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<BloodBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: BloodBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code ==0 && t.data!=null) {
                            mView?.getBloodPressListSuccess(t)
                        } else {
                            mView?.showError(t?.message)
                        }
                    }
                })
        )
    }

}